'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 士兵
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var dataApi = require('../libs/dataApi');
var cacheUtil = require('../util/cacheUtil.js');

/**
 * 获取士兵类型配置
 * @param  {[type]} soldier_type [description]
 * @param  {[type]} lv           [description]
 * @return {[type]}              [description]
 */
exports.getSoldierTypeConf = function (soldier_type) {
    return _getSoldierTypeConf(soldier_type);
};

/**
 * 获取士兵星级取出配置
 * @param  {[type]} soldier_type [description]
 * @param  {[type]} lv           [description]
 * @return {[type]}              [description]
 */
exports.getSoldierStarConf = function (soldier_type, soldier_star) {
    return _getSoldierStarConf(soldier_type, soldier_star);
};

var _getSoldierStarConf = cacheUtil.memorize(function (soldier_type, soldier_star) {
    var soldier_conf = dataApi.soldier_star.findById(soldier_type);
    if (!soldier_conf) {
        console.error('soldier type can not found:' + soldier_type);
        return null;
    }
    for (var i = 0; i < soldier_conf.length; i++) {
        var soldier_star_conf = soldier_conf[i];
        if (soldier_star_conf.star == soldier_star) {
            return soldier_star_conf;
        }
    }
    console.error('soldier star can not found:' + soldier_type + ',' + soldier_star);
    return null;
});

/**
 * 获取士兵等级配置
 * @param  {[type]} soldier_type [description]
 * @param  {[type]} lv           [description]
 * @return {[type]}              [description]
 */
exports.getSoldierLvConf = function (soldier_lv) {
    var soldier_conf = dataApi.soldier_lvl.findById(soldier_lv);
    if (!soldier_conf) {
        console.error('soldier lv can not found:' + soldier_lv);
        return null;
    }
    return soldier_conf;
};

exports.getSoldierProductTime = function (soldier_type) {
    var soldier_type_conf = this.getSoldierTypeConf(soldier_type);
    if (!soldier_type_conf) {
        console.error('soldier_type_conf type can not found:' + soldier_type);
        return 0;
    }
    return soldier_type_conf.time_cost;
};

var _getSoldierTypeConf = function (soldier_type) {
    return dataApi.soldier_type.findById(soldier_type);
};

/**
 * 获取士兵天赋配置
 * @param  {[type]} soldier_type [description]
 * @return {[type]}              [description]
 */
exports.getSoldierTalentConf = function (soldier_type) {
    var talent_list = dataApi.soldier_talent_config.findById(soldier_type);
    if (!talent_list) {
        console.error('soldier type can not found:' + soldier_type);
        return null;
    }
    return talent_list;
};

/**
 * 获取士兵天赋ID列表
 * @param  {[type]} soldier_type [description]
 * @return {[type]}              [description]
 */
exports.getTalentTypeLvList = function (soldier_type) {
    var talent_list = this.getSoldierTalentConf(soldier_type);
    if (!talent_list) {
        console.error('soldier type can not found:' + soldier_type);
        return [];
    }
    var id_list = [];
    _.forEach(talent_list, function (item) {
        id_list.push(item.talent_id * 100);
    });
    return id_list;
};

/**
 * 获取士兵天赋等级信息
 * @param  {[type]} talent_lv_id [天赋ID * 100 + 等级]
 * @return {[type]}              [description]
 */
exports.getSoldierTalentLvConf = function (talent_type_lv) {
    var talent_conf = dataApi.soldier_talent_type.findById(talent_type_lv);
    if (!talent_conf) {
        console.error('天赋类型+等级 无效:' + talent_type_lv);
        return null;
    }
    return talent_conf;
};

/**
 * 获取升级天赋消耗材料
 * @param  {[type]} talent_lv_id [天赋ID * 100 + 等级]
 * @return {[type]}              [description]
 */
exports.getUpgradeTalentCostConf = function (star_lv, talent_lv) {
    var key = star_lv * 100 + talent_lv;
    var talent_cost_conf = dataApi.soldier_talent_cost.findById(key);
    if (!talent_cost_conf) {
        console.error('士兵星级+天赋等级 无效:' + key);
        return null;
    }

    return talent_cost_conf;
};

var ratio_cache = {};
/**
 * 获取天赋系数
 * @param  {[type]} talent_lv_id [天赋ID * 100 + 等级]
 * @return {[type]}              [description]
 */
exports.getTalentRatio = function (soldier_type, star_lv) {
    var key = soldier_type * 100 + star_lv;
    if (key in ratio_cache) {
        return ratio_cache[key];
    }
    var talent_conf = dataApi.soldier_talent_config.findById(key);
    if (!talent_conf) {
        console.error('士兵类型+星级 无效:' + key);
        return 100;
    }
    ratio_cache[key] = talent_conf.ratio;
    return ratio_cache[key];
};
